package ihm.widgets;

import ihm.tabs.PicturesTab;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import tools.ImageUtils;

public class Picture extends Composite
{

	private final int MAX_HEIGHT = 200;
	private final int MAX_WIDTH = 100;

	private Color borderColor;
	private Label imgContainer;
	private Label usernameContainer;
	
	private String pID;
	private String aID;
	private String albumName;

	public Picture( Composite parent, String pictureName, Image picture, String pID, String aID, String albumName )
	{
		super( parent, SWT.None );
		this.pID = pID;
		this.aID = aID;
		this.albumName = albumName;

		//////////////////////////////////////////////
		// general composite style
		/////////////////////////////////////////////

		RowLayout layout = new RowLayout();
		layout.type = SWT.VERTICAL;
		layout.center = true;
		layout.marginWidth = 1;
		layout.marginHeight = 1;
		setLayout(layout);

		this.borderColor = getBackground();
		this.addPaintListener(new PaintListener() {
			@Override
			public void paintControl(PaintEvent e) {
				e.gc.setForeground(borderColor);
				e.gc.drawRectangle(0, 0, getBounds().width - 1, getBounds().height - 1);
			}
		});

		/////////////////////////////////////////////////
		// Set controls data
		/////////////////////////////////////////////////
		imgContainer = new Label( this, SWT.CENTER );
		imgContainer.setSize( MAX_WIDTH, MAX_HEIGHT );

		ImageData resizedPictureData = ImageUtils.scaleImage( picture, MAX_HEIGHT, MAX_WIDTH);
		Image profilePicture = new Image(getDisplay(), resizedPictureData);
		imgContainer.setImage(profilePicture);

		usernameContainer = new Label(this, SWT.CENTER);
		usernameContainer.setText( pictureName );

		///////////////////////////////////////////////////////////////////
		// Setup event handling
		/////////////////////////////////////////////////////////

		MouseTrackAdapter mouseTrackAdapter = new MouseTrackAdapter() {

			@Override
			public void mouseExit(MouseEvent e) {
				setBorderColor(getBackground());
			}

			@Override
			public void mouseEnter(MouseEvent e) {
				setBorderColor(getDisplay().getSystemColor(SWT.COLOR_TITLE_BACKGROUND));
			}
		};

		MouseAdapter mouseAdapter = new MouseAdapter() {

			@Override
			public void mouseDown( MouseEvent e ) {
			}

			@Override
			public void mouseUp( MouseEvent e ) {
			}

			@Override
			public void mouseDoubleClick( MouseEvent e ) {
				((PicturesTab)getParent().getParent()).setLayoutToPicture( getpID(), getaID(), getAlbumName() );
			}
		};

		usernameContainer.addMouseTrackListener(mouseTrackAdapter);
		imgContainer.addMouseTrackListener(mouseTrackAdapter);

		usernameContainer.addMouseListener(mouseAdapter);
		imgContainer.addMouseListener(mouseAdapter);
	}

	/**
	 * Mouse events are fired when the image or the caption
	 * is clicked.
	 * @param listener Mouse event listener
	 */
	@Override
	public void addMouseListener(MouseListener listener)
	{
		usernameContainer.addMouseListener(listener);
		imgContainer.addMouseListener(listener);
	}

	private void setBorderColor(Color color)
	{
		this.borderColor = color;
		redraw();
	}

	public String getpID() {
		return pID;
	}

	public String getaID() {
		return aID;
	}

	public String getAlbumName() {
		return albumName;
	}

}
